﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// Summary description for TanashCMSHttpHandlerFactory
/// </summary>
public class TanashCMSHttpHandlerFactory : IHttpHandlerFactory
{
    public TanashCMSHttpHandlerFactory()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region IHttpHandlerFactory Members

    public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
    {
        if (File.Exists(pathTranslated))
        {
            IHttpHandler handler = new System.Web.DefaultHttpHandler();
            return handler;
        }

        string filePath = context.Request.Path;
        string pageName = Path.GetFileNameWithoutExtension(filePath);
        filePath = filePath.Substring(0, filePath.IndexOf("/" + pageName));
        string pageXPath = "";
        if (filePath.Length > 0)
            pageXPath = filePath.Substring(context.Request.ApplicationPath.TrimEnd('/').Length) + "/" + pageName;
        else
            pageXPath = "/" + pageName;
        //string pageXPath = filePath.Substring(context.Request.ApplicationPath.Length) + "/" + pageName;
        context.Items["pageXPath"] = pageXPath;

        string processorPath = context.Server.MapPath(context.Request.ApplicationPath.TrimEnd('/') + "/default.aspx");

        return PageParser.GetCompiledPageInstance(url, processorPath, context);
    }

    public void ReleaseHandler(IHttpHandler handler)
    {
        
    }

    #endregion
}
